package com.linsir.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsir.config.SystemConfig;
import com.linsir.lin.dto.*;
import com.linsir.lin.mapper.SysMenuMapper;
import com.linsir.lin.pojo.Account;
import com.linsir.lin.pojo.SysMenu;
import com.linsir.lin.service.AccountService;
import com.linsir.lin.service.RoleService;
import com.linsir.lin.service.SysMenuService;
import com.linsir.utils.TreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;


@Service
@Slf4j
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper,SysMenu> implements SysMenuService,IService<SysMenu>{


    @Resource
    private SystemConfig systemConfig;


    @Autowired
    private RoleService roleService;

    @Autowired
    private AccountService accountService;

    /**
     * 转化DTO
     * @param sysMenu
     * @return
     */
    @Override
    public SysMenuDTO PoJOtoDTO(SysMenu sysMenu) {
        SysMenuDTO sysMenuDTO =new SysMenuDTO();
        sysMenuDTO.setId(sysMenu.getId());
        sysMenuDTO.setPid(sysMenu.getPid());
        sysMenuDTO.setTitle(sysMenu.getTitle());
        sysMenuDTO.setTarget(sysMenu.getTarget());
        sysMenuDTO.setHref(sysMenu.getHref());
        sysMenuDTO.setIcon(sysMenu.getIcon());
        sysMenuDTO.setStatus(sysMenu.getStatus());
        sysMenuDTO.setRemark(sysMenu.getRemark());
        sysMenuDTO.setSort(sysMenu.getSort());
        return sysMenuDTO;
    }

    /**
     * 菜单列表，不做分页，全部查询出来
     *
     * @return
     * @throws IOException
     */
    @Override
    public GridDTO<SysMenu> grid() throws IOException {

        List<SysMenu> list =list();
        Integer size =list.size();
        return new GridDTO<SysMenu>("0","成功",size.longValue(),list);
    }


    /**
     * 获取menuInfo
     * @return
     */

    @Override
    public List<SysMenuDTO> menuInfo() {
        List<SysMenuDTO> menuInfo = new ArrayList<SysMenuDTO>();

        QueryWrapper queryWrapper = new  QueryWrapper<SysMenu> ();
        queryWrapper.orderByAsc("sort");
        List<SysMenu> sysMenuList = list(queryWrapper);

        for (SysMenu sysMenu : sysMenuList)
        {
            SysMenuDTO sysMenuDTO = new SysMenuDTO();
            sysMenuDTO = PoJOtoDTO(sysMenu);
            menuInfo.add(sysMenuDTO);
        }
        return TreeUtil.toTree(menuInfo,0);
    }

    @Override
    public Map<String, Object> initData() {
        Account account = (Account) SecurityUtils.getSubject().getPrincipal();
        log.info(account.getUserName());
        AtomicBoolean isEngineer = new AtomicBoolean(false);

        accountService.rolesByAccount(account).forEach(r->{
            if (r.getGroup().equals("1"))
            {
                isEngineer.set(true);
            }
        });


        Map<String,Object> map = new HashMap<String,Object>();
        HomeInfoDTO homeInfo= new HomeInfoDTO();
        homeInfo.setTitle(systemConfig.getIndexPageName());
        if (isEngineer.get())
        {
            homeInfo.setHref("/admin/engineerIndex");
            homeInfo.setTitle("我的工作台");
        }
        else {
            homeInfo.setHref("/admin/index");
        }

        LogoInfoDTO logoInfo = new LogoInfoDTO();
        logoInfo.setTitle(systemConfig.getTitle());
        logoInfo.setImage(systemConfig.getLogo());
        logoInfo.setHref("/admin");
        List<SysMenuDTO> menuInfo=menuInfo();
        map.put("homeInfo",homeInfo);
        map.put("logoInfo",logoInfo);
        map.put("menuInfo",menuInfo);
        return  map;
    }


    public boolean saveSysMenu()
   {
       SysMenu sysMenu =new SysMenu();
       sysMenu.setPid(0);
       sysMenu.setIcon("fa fa-address-book");
       sysMenu.setSort(0);
       sysMenu.setStatus(true);
       sysMenu.setTarget("_self");
       sysMenu.setRemark("测试惨淡");
      return save(sysMenu);
   }

    @Override
    public boolean addSysMenu(SysMenuDTO sysMenuDTO) {
        return false;
    }



    @Override
    public List<SysMenu> sysMenuList() {
        return list();
    }

    @Override
    public List<SysMenu> getChildByPid(Integer pid) {
        QueryWrapper<SysMenu> sysMenuQueryWrapper =new QueryWrapper<SysMenu>();
        sysMenuQueryWrapper.eq("pid",pid);
        return list(sysMenuQueryWrapper);
    }

    @Override
    public SysMenu DTOtoPoJO(SysMenuDTO sysMenuDTO) {
        return null;
    }




}
